<include file="common:__head__"/>
<div class="permissions-content">
    <form class="form-horizontal" id="add_member" action="{:U('Members/add_member')}" method="post">
        <div class="permissions-le">
            <span class="add-admin"><a href="javascript:history.go(-1)"></a>添加客户</span>
            <div class="add-admin-ipt">
                <span><i>*</i>客户姓名:</span>
                <input type="text" id="c_name" name="member_name" placeholder="请填写客户姓名" class="w200"/>
            </div>
            <div class="add-admin-ipt">
                <span><i>*</i>客户手机:</span>
                <input type="text" name="mobile" id="mobile" data-type="number" maxlength="11" placeholder="请填写手机号码" class="w200"/>
                <span id="err_mobile" style="margin-left:10px;"></span>
            </div>
            <div class="add-admin-ipt">
                <span><i>*</i>客户微信:</span>
                <input type="text" name="wx" id="wx" placeholder="手机微信必填一个" class="w200"/>
            </div>
            <div class="add-admin-ipt">
                <span>客户地址:</span>
                <input type="text" name="address" id="address" placeholder="请填写客户地址" class="w200"/>
            </div>
            <div class="add-admin-ipt">
                <span>分配给:</span>
                <input type="text" id="search_to" placeholder="填写代理姓名搜索" style="margin-right:5px;"/>
                <select id="to" name="agent_id">
                    <option value="0">请选择</option>
                </select>
            </div>
            <div class="add-admin-ipt">
                <span></span>
                <a class="btn-lg btn-blue" id="submitBtn">保存</a>
            </div>
        </div>
    </form>
    <div class="permissions-ri">
        <a href="" class="permissions-help">帮助和服务</a>
    </div>
</div>
<script type="text/javascript">
    $(function(){
        $('#submitBtn').click(function () {
            var c_name = $('#c_name').val();
            var mobile = $('#mobile').val();
            var wx = $("#wx").val();
            var to = $("#to").val();
            if (c_name == '') {
                layer.alert('客户名称必填！');
                return false;
            }
            var mobilePattern = /^0?1[3|4|5|8|7][0-9]\d{8}$/;
            if(mobile != ''){
                if (!mobilePattern.test(mobile)) {
                    layer.alert('请输入正确的手机号码');
                    return false;
                }
            }
            if (wx == ''&&mobile == '') {
                layer.alert('客户手机与微信至少填一个!');
                return false;
            }
            $("#add_member").submit();
        });
        $("#search_to").keyup(function(){
            $aName = $(this).val();
            if($aName != ''){
                getAgentName();
            }else{
                return false;
            }
        });
        $("#search_to").change(function(){
            $aName = $(this).val();
            if($aName != ''){
                getAgentName();
            }else{
                return false;
            }
        });
        function getAgentName()
        {
            $.ajax({
                url: "{:U('Members/searchAgent')}",
                type: "post",
                data: {aName : $aName},
                success: function (data) {
                    if (data) {
                        options = "";
                        $.each(data, function(k, v){
                            options += '<option value="'+ v.agent_id +'">'+ v.agent_name +' '+v.mobile+'</option>';
                        });
                        $("#to").html(options);
                    }
                }
            });
        }
    });
</script>
<include file="common:__foot__"/>